package GameMain.Model
{
	import GameMain.Game;
	import GameMain.Macro.Macro;
	import GameMain.Macro.MacroEvent;
	
	import Interface.Common.IEntityManager;
	
	import flash.utils.Dictionary;
	
	import library.Function.Dict;
	import library.SocketData.SocketData;
	
	/*
	游戏中心实体管理
	*/
	
	public class EntityManager implements IEntityManager
	{
		
		private var _entityDict:Dict = new Dict;
		private var _ownPlayer:OwnPlayer = null;
		
		public function EntityManager()
		{
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_SOCKET2_GAME,onSuccessGame);
			Game.EngineMgr.registerEvent(MacroEvent.MSG_FROM_SERVER_RESET_OWN_ENTITY,resetOwnPlayerEntityID);
		}
		
		public function get ownPlayer():OwnPlayer
		{
			return this._ownPlayer;
		}
		
		public function createOwnPlayer(data:SocketData):void
		{
			_ownPlayer = new OwnPlayer();
			_ownPlayer.initEntityProp(data);
			_entityDict.dictionary[_ownPlayer.entityID] = _ownPlayer;
		}
		
		public function insertEntity(entity:EntityMove):void
		{
			this._entityDict.dictionary[entity.entityID] = entity;
		}
		
		private function resetOwnPlayerEntityID(data:SocketData):void
		{
			this._ownPlayer.setEntityID(data.readUInt16());
			this.resetEntity();
		}
		
		public function getEntity(entityID:int):EntityMove
		{
			return _entityDict.dictionary[entityID];
		}
		
		public function removeEntity(entityID:int):void
		{
			var entity:EntityMove = this._entityDict.pop(entityID) as EntityMove;
			if(entity != null)
			{
				entity.destroy();
			}
		}
		
		private function onSuccessGame(data:SocketData):void
		{
			var player:Player;	var entID:int; var seatIndex:int;
			entID = data.readInt16();
			seatIndex = data.readInt8();
			if(entID == this._ownPlayer.entityID)
			{
				player = this._ownPlayer;
			}
			else
			{
				player = new Player();
				
			}
			player.initGameProp(entID,data);
			player.setPlayerTeamID(seatIndex);
			_entityDict.dictionary[player.entityID] = player;
			Game.ModuleMgr.getModule(Macro.MODULE_WAIT_GAME)["setTeamItem"](player);
			if(entID == this._ownPlayer.entityID)
			{
				Game.ModuleMgr.getModule(Macro.MODULE_WAIT_GAME)["onLoadIngGame"]();
			}
			
		}
		
		
		public function startNewPlayerTeach():void
		{
			this._ownPlayer.setPlayerTeamID(11);
			
			var p:Player = new Player();
			p.setPlayerTeamID(12);
			var data:SocketData = new SocketData();
			data.writeInt16(100);
			data.writeInt16(0);
			data.writeInt8(1);
			data.writeInt8(1);
			data.writeInt8(2);
			data.writeString("教学员");
			data.position = 0;
			p.initGameProp(100,data);
			_entityDict.dictionary[p.entityID] = p;
			
			Game.newPlayerTH.start();
		}
		
		public function resetEntity():void
		{
			for(var i:* in this._entityDict.dictionary)
			{
				var entity:EntityMove = this._entityDict.dictionary[i] as EntityMove;
				if(entity.entityID == _ownPlayer.entityID)
					continue;
				entity.destroy();
			}
			this._entityDict = new Dict();
			this._entityDict.dictionary[_ownPlayer.entityID] = _ownPlayer;
			
		}
		
		public function get entityList():Dictionary
		{
			return this._entityDict.dictionary;
		}
		
		
	}
}